<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.username" placeholder="学生姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.phone" placeholder="学生电话"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleEdit">新增</el-button>
				</el-form-item>

			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="studentName" label="姓名" width="100"></el-table-column>
			<el-table-column prop="studentPhone" label="手机号" width="120" ></el-table-column>
			<!--<el-table-column prop="classname" label="班级" width="150"></el-table-column>-->
			<el-table-column prop="originalClass" label="原班级" width="120"></el-table-column>
			<el-table-column prop="presentClass" label="目前班级" width="150"></el-table-column>
			<!--<el-table-column prop="repeatReason" label="留级原因" width="180"></el-table-column>-->
			<!--<el-table-column prop="remark" label="备注" width="180"></el-table-column>-->
			<el-table-column label="留级原因"  :show-overflow-tooltip="true" >
				<template slot-scope="scope">
					<span v-html="scope.row.repeatReason"></span>
				</template>
			</el-table-column>
			<el-table-column label="备注"  :show-overflow-tooltip="true" >
				<template slot-scope="scope">
					<span v-html="scope.row.remark"></span>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" >
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm" >
				<el-row>
					<el-col :span="12">
						<el-form-item label="姓名" prop="studentName">
							<el-input v-model="editForm.studentName" auto-complete="off" style="width: 180px;height: 35px;" max-length="20"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="电话" prop="studentPhone">
							<el-input v-model="editForm.studentPhone" auto-complete="off" style="width: 180px;height: 35px;" max-length="20"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="原班级" prop="originalClass">
							<el-input v-model="editForm.originalClass" auto-complete="off" style="width: 180px;height: 35px;"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="目前班级" prop="presentClass">
							<el-input v-model="editForm.presentClass" auto-complete="off" style="width: 180px;height: 35px;"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<!--<el-form-item label="留级原因" prop="repeatReason">-->
					<!--<el-input type="input" v-model="editForm.repeatReason"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item label="备注" prop="remark">-->
					<!--<el-input type="input" v-model="editForm.remark"></el-input>-->
				<!--</el-form-item>-->
				<el-form-item label="留级原因" prop="repeatReason">
					<!--<el-input v-model="editForm.question" auto-complete="off"></el-input>-->
					<quill-editor  v-model="editForm.repeatReason"  :options="editorOption"   >
						<!-- 自定义toolar -->
						<div id="toolbar" slot="toolbar">
							<!-- Add a bold button -->
							<button class="ql-bold" title="加粗">Bold</button>
							<button class="ql-italic" title="斜体">Italic</button>
							<button class="ql-underline" title="下划线">underline</button>
							<button class="ql-strike" title="删除线">strike</button>
							<button class="ql-blockquote" title="引用"></button>
							<button class="ql-code-block" title="代码"></button>
							<button class="ql-header" value="1" title="标题1"></button>
							<button class="ql-header" value="2" title="标题2"></button>
							<!--Add list -->
							<button class="ql-list" value="ordered" title="有序列表"></button>
							<button class="ql-list" value="bullet" title="无序列表"></button>
							<!-- Add font size dropdown -->
							<select class="ql-header" title="段落格式">
								<option selected>段落</option>
								<option value="1">标题1</option>
								<option value="2">标题2</option>
								<option value="3">标题3</option>
								<option value="4">标题4</option>
								<option value="5">标题5</option>
								<option value="6">标题6</option>
							</select>
							<select class="ql-size" title="字体大小">
								<option value="10px">10px</option>
								<option value="12px">12px</option>
								<option value="14px">14px</option>
								<option value="16px" selected>16px</option>
								<option value="18px">18px</option>
								<option value="20px">20px</option>
							</select>
							<select class="ql-font" title="字体">
								<option value="SimSun">宋体</option>
								<option value="SimHei">黑体</option>
								<option value="Microsoft-YaHei">微软雅黑</option>
								<option value="KaiTi">楷体</option>
								<option value="FangSong">仿宋</option>
								<option value="Arial">Arial</option>
							</select>
							<!-- Add subscript and superscript buttons -->
							<select class="ql-color" value="color" title="字体颜色"></select>
							<select class="ql-background" value="background" title="背景颜色"></select>
							<select class="ql-align" value="align" title="对齐"></select>
							<button class="ql-clean" title="还原"></button>
							<!-- You can also add your own -->
						</div>
					</quill-editor>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<!--<el-input v-model="editForm.suggestion" auto-complete="off"></el-input>-->
					<quill-editor  v-model="editForm.remark"  :options="editorOptionRemark"   >
						<!-- 自定义toolar -->
						<div id="toolbar1" slot="toolbar">
							<!-- Add a bold button -->
							<button class="ql-bold" title="加粗">Bold</button>
							<button class="ql-italic" title="斜体">Italic</button>
							<button class="ql-underline" title="下划线">underline</button>
							<button class="ql-strike" title="删除线">strike</button>
							<button class="ql-blockquote" title="引用"></button>
							<button class="ql-code-block" title="代码"></button>
							<button class="ql-header" value="1" title="标题1"></button>
							<button class="ql-header" value="2" title="标题2"></button>
							<!--Add list -->
							<button class="ql-list" value="ordered" title="有序列表"></button>
							<button class="ql-list" value="bullet" title="无序列表"></button>
							<!-- Add font size dropdown -->
							<select class="ql-header" title="段落格式">
								<option selected>段落</option>
								<option value="1">标题1</option>
								<option value="2">标题2</option>
								<option value="3">标题3</option>
								<option value="4">标题4</option>
								<option value="5">标题5</option>
								<option value="6">标题6</option>
							</select>
							<select class="ql-size" title="字体大小">
								<option value="10px">10px</option>
								<option value="12px">12px</option>
								<option value="14px">14px</option>
								<option value="16px" selected>16px</option>
								<option value="18px">18px</option>
								<option value="20px">20px</option>
							</select>
							<select class="ql-font" title="字体">
								<option value="SimSun">宋体</option>
								<option value="SimHei">黑体</option>
								<option value="Microsoft-YaHei">微软雅黑</option>
								<option value="KaiTi">楷体</option>
								<option value="FangSong">仿宋</option>
								<option value="Arial">Arial</option>
							</select>
							<!-- Add subscript and superscript buttons -->
							<select class="ql-color" value="color" title="字体颜色"></select>
							<select class="ql-background" value="background" title="背景颜色"></select>
							<select class="ql-align" value="align" title="对齐"></select>
							<button class="ql-clean" title="还原"></button>
							<!-- You can also add your own -->
						</div>
					</quill-editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" style="text-align:center;">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import {  removeTeacher, batchRemoveTeacher, editTeacher } from '../../api/api';

    import {
        Quill,
        quillEditor
    } from 'vue-quill-editor'
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    //引入font.css
    import '../../css/font.css'
    import ElRow from "element-ui/packages/row/src/row";

    // 自定义字体大小
    let Size = Quill.import('attributors/style/size')
    Size.whitelist = ['10px', '12px', '14px', '16px', '18px', '20px']
    Quill.register(Size, true)

    // 自定义字体类型
    var fonts = ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial', 'Times-New-Roman', 'sans-serif',
        '宋体', '黑体'
    ]
    var Font = Quill.import('formats/font')
    Font.whitelist = fonts
    Quill.register(Font, true)

	export default {
        components: {
            ElRow,
            quillEditor},
		data() {
			return {
                editorOption: {
                    placeholder: "请输入",
                    theme: "snow", // or 'bubble'
                    modules: {
                        toolbar: {
                            container: '#toolbar'
                        }
                    }
                },
                editorOptionRemark:{
                    placeholder: "请输入",
                    theme: "snow", // or 'bubble'
                    modules: {
                        toolbar: {
                            container: '#toolbar1'
                        }
                    }
                },
				filters: {
                    username: '',
					phone:''
				},
				options:[],
				users: [],
				total: 0,
				currentPage: 1,
				pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
                    studentName: [
                        { required: true, message: '请输入姓名', trigger: 'blur' },
                    ],
                    studentPhone:[
                        { required: true, message: '请填写电话', trigger: 'blur' }
                    ],
                    originalClass: [
                        { required: true, message: '请输入原班级', trigger: 'blur' },
                    ],
                    presentClass:[
                        { required: true, message: '请填目前班级', trigger: 'blur' }
                    ],
                    repeatReason: [
                        { required: true, message: '请输入留级原因', trigger: 'blur' },
                    ],
                    remark:[
                        { required: true, message: '请填写备注', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {

				},
			}
		},
		methods: {
            checkPhone(value){
            	if (!(/^1[34578]\d{9}$/.test(value))) {
                	alert("请输入正确的手机号码!");
                	return false;
                }
                return true;
            },
			getClassList(){
				this.$http.post("/class/classList").then((res) => {
					this.options = res.data;
				});
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				let para = {
					currentPage: this.currentPage,
					pageSize:this.pageSize,
                    username: this.filters.username,
					phone: this.filters.phone
				};
				this.listLoading = true;
				this.$http.patch("/repeat", para).then((res) => {
					this.total = res.data.data.total;
					this.users = res.data.data.list;
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
                    this.$http.delete("/repeat/"+row.id).then((res) => {
						this.listLoading = false;
						if(res.data.stuts){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						}else{
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},

			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							let flag = this.checkPhone(para.studentPhone);
							if(flag == false){
								this.editLoading = false;
								return;
							}
							if(para.id){
                                this.$http.post("/repeat", para).then((res) => {
                                    this.editLoading = false;
                                    if(res.data.stuts){
                                        //NProgress.done();
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                        this.$refs['editForm'].resetFields();
                                        this.editFormVisible = false;
                                        this.getUsers();
                                    }else{
                                        this.$message({
                                            message: res.data,
                                            type: 'error'
                                        });
                                    }

                                });
							}else{
                                this.$http.put("/repeat", para).then((res) => {
                                    this.editLoading = false;
                                    if(res.data.stuts){
                                        //NProgress.done();
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                        this.$refs['editForm'].resetFields();
                                        this.editFormVisible = false;
                                        this.getUsers();
                                    }else{
                                        this.$message({
                                            message: res.data,
                                            type: 'error'
                                        });
                                    }
                                });
							}
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/repeat/batch",ids).then((res) => {
						if(res.data.stuts){
							this.listLoading = false;
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getUsers();
						}else{
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
			this.getClassList();
		}
	}

</script>

<style scoped>

</style>